package com.itmumu.miniwechat.common.domain.response;

import com.itmumu.miniwechat.common.exception.CommonErrorEnum;
import com.itmumu.miniwechat.common.exception.ErrorEnum;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.apache.ibatis.javassist.runtime.Desc;

import java.io.Serializable;

/**
 * Description: 通用返回体
 * Author: <a href="https://github.com/zongzibinbin">abin</a>
 * Date: 2023-03-23
 */
@Data
@Schema(description = "基础返回体")
public class ApiResult<T> implements Serializable {

    @Schema(description = "错误码")
    private Integer errCode;
    @Schema(description = "错误消息")
    private String message;
    @Schema(description = "返回对象")
    private T data;

    public static <T> ApiResult<T> success() {
        ApiResult<T> result = new ApiResult<T>();
        result.setErrCode(CommonErrorEnum.SUCCESS.getCode());
        result.setMessage(CommonErrorEnum.SUCCESS.getMessage());
        result.setData(null);

        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<T>();
        result.setData(data);
        result.setErrCode(CommonErrorEnum.SUCCESS.getCode());
        result.setMessage(CommonErrorEnum.SUCCESS.getMessage());
        return result;
    }

    public static <T> ApiResult<T> fail(Integer code, String msg) {
        ApiResult<T> result = new ApiResult<T>();
        result.setErrCode(code);
        result.setMessage(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(ErrorEnum errorEnum) {
        ApiResult<T> result = new ApiResult<T>();
        result.setErrCode(errorEnum.getCode());
        result.setMessage(errorEnum.getMessage());
        return result;
    }

    public ApiResult<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public ApiResult<T> code(Integer code) {
        this.setErrCode(code);
        return this;
    }

}
